package ecustom.common.util;

import java.io.*;

import org.apache.log4j.Logger;

public class IOUtil {

    private static final Logger log = Logger.getLogger(IOUtil.class);

    private static final int EOF = -1;
    private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;

    public static void close(InputStream stream) {
        if (stream == null) return;
        try {
            stream.close();
        } catch (Exception e) {
            log.error("close() " + stream.getClass() + " 流关闭失败", e);
        }
    }
    
    public static void close(OutputStream stream) {
        if (stream == null) return;
        try {
            stream.close();
        } catch (Exception e) {
            log.error("close() " + stream.getClass() + " 流关闭失败", e);
        }
    }
    
    public static void close(Reader stream) {
        if (stream == null) return;
        try {
            stream.close();
        } catch (Exception e) {
            log.error("close() " + stream.getClass() + " 流关闭失败", e);
        }
    }

    /**
     * 复制文件流，从 inputStream 至 outputStream。
     * <p>源自：org.apache.commons.io.IOUtils.copy()</p>
     * @param input
     * @param output
     * @return
     * @throws IOException
     */
    public static int copy(InputStream input, OutputStream output) throws IOException {
        long count = copyLarge(input, output);
        if (count > Integer.MAX_VALUE) {
            return -1;
        }
        return (int) count;
    }

    /**
     * 复制文件流，从 inputStream 至 outputStream。
     * <p>源自：org.apache.commons.io.IOUtils.copyLarge()</p>
     * @param input
     * @param output
     * @return
     * @throws IOException
     */
    public static long copyLarge(InputStream input, OutputStream output)
            throws IOException {
        return copyLarge(input, output, new byte[DEFAULT_BUFFER_SIZE]);
    }

    /**
     * 复制文件流，从 inputStream 至 outputStream。
     * <p>源自：org.apache.commons.io.IOUtils.copyLarge()</p>
     * @param input
     * @param output
     * @param buffer
     * @return
     * @throws IOException
     */
    public static long copyLarge(InputStream input, OutputStream output, byte[] buffer)
            throws IOException {
        long count = 0;
        int n = 0;
        while (EOF != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
            count += n;
        }
        return count;
    }

    /**
     * 将文件输入流转成字节数组并返回。
     * <p>源自：org.apache.commons.io.IOUtils.toByteArray()</p>
     * @param input
     * @return
     * @throws IOException
     */
    public static byte[] toByteArray(InputStream input) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        copy(input, output);
        return output.toByteArray();
    }

    public static byte[] toByteArray(File file) throws IOException {
        try (InputStream is = new FileInputStream(file)) {
           return toByteArray(is);
        }
    }

    public static byte[] toByteArray(String filePath) throws IOException {
        return toByteArray(new File(filePath));
    }
}